package a66weding.com.jiehuntong.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.WriteStoryActivity;
import a66weding.com.jiehuntong.beans.ImageStoryBean;
import a66weding.com.jiehuntong.common.utils.Tools;
import a66weding.com.jiehuntong.fragment.ImageStoryFragment;

public class AMDragRateAdapter extends BaseAdapter {

    private WriteStoryActivity context;
    List<ImageStoryBean.DataBean> items;//适配器的数据源
    deleteCallback dc;
    ImageStoryFragment fr;
    public AMDragRateAdapter(WriteStoryActivity context, List<ImageStoryBean.DataBean> list , deleteCallback dc, ImageStoryFragment fr){
        this.context = (WriteStoryActivity) context;
        this.items = list;
        this.dc=dc;
        this.fr =fr;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public ImageStoryBean.DataBean getItem(int arg0) {
        return items.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }
    public void remove(int arg0) {//删除指定位置的item
        items.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    public void insert(ImageStoryBean.DataBean item, int arg0) {//在指定位置插入item
        items.add(arg0, item);
        this.notifyDataSetChanged();
    }

    ViewHolder viewHolder;
    @Override
    public View getView(final int position,  View convertView, final ViewGroup parent) {
        final ImageStoryBean.DataBean item = (ImageStoryBean.DataBean)getItem(position);
        //绑定复用(为米4适配取消复用)
//        if(convertView==null){
//            viewHolder = new ViewHolder();
//            convertView = LayoutInflater.from(context).inflate(R.layout.item_img_story, null);
//            viewHolder.et_blessing = (EditText) convertView.findViewById(R.id.et_blessing);
//            viewHolder.img_love = (ImageView) convertView.findViewById(R.id.img_love);
//            viewHolder.img_remove = (ImageView) convertView.findViewById(R.id.img_deleteitem);
//            viewHolder.img_order = (ImageView) convertView.findViewById(R.id.img_order);
//            convertView.setTag(viewHolder);
//        }else{
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.item_img_story, null);
            viewHolder.et_blessing = (EditText) convertView.findViewById(R.id.et_blessing);
            viewHolder.img_love = (ImageView) convertView.findViewById(R.id.img_love);
            viewHolder.img_remove = (ImageView) convertView.findViewById(R.id.img_deleteitem);
            viewHolder.img_order = (ImageView) convertView.findViewById(R.id.img_order);
            convertView.setTag(viewHolder);


        viewHolder.et_blessing.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (v.getId()) {
                    case R.id.et_blessing:
                        if(v instanceof EditText && ((EditText)v).getLineCount() > 3){
                            v.getParent().requestDisallowInterceptTouchEvent(true);
                        }
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_UP:
                                v.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                        }
                }
                return false;
            }
        });


        //删除按钮事件回调
        viewHolder.img_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 InputMethodManager soft= (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
                soft.hideSoftInputFromWindow( viewHolder.et_blessing.getWindowToken(), 0);;
                Tools.showSimpleDialog(context, "确认删除此图片故事", "取消", "删除", new Tools.ClickListenerInterface(){
                    @Override
                    public void doConfirm() {
                        dc.deleteItem(position);
                    }
                    @Override
                    public void doCancel() {
                    }
                });

            }
        });
        //当前是否可编辑
        if (context.isEdit){
            viewHolder.et_blessing.getLayoutParams().width=ViewGroup.LayoutParams.WRAP_CONTENT;
            viewHolder.et_blessing.setMaxEms(5);
            viewHolder.et_blessing.setEllipsize(TextUtils.TruncateAt.END);
            viewHolder.img_remove.setVisibility(View.GONE);
            viewHolder.img_order.setVisibility(View.VISIBLE);

        }else {
            viewHolder.et_blessing.getLayoutParams().width=ViewGroup.LayoutParams.MATCH_PARENT;
            viewHolder.et_blessing.setEllipsize(TextUtils.TruncateAt.END);
            viewHolder.et_blessing.setMaxEms(10);
            viewHolder.img_remove.setVisibility(View.VISIBLE);
            viewHolder.img_remove.setFocusable(true);
            viewHolder.img_order.setVisibility(View.GONE);
        }
        //设置内容
        viewHolder.et_blessing.setText(item.getNvc_PicStory());
        viewHolder.et_blessing.setTag(position);
        viewHolder.et_blessing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                item.setNvc_PicStory(s.toString());
            }
        });
        //加载图片
        Glide.with(context).load(item.getNvc_Pic()).placeholder(R.mipmap.bg_loadimage).into( viewHolder.img_love);
        return convertView;
    }

    public interface deleteCallback{
        public void   deleteItem(int position);
    }

    class ViewHolder {
        EditText et_blessing;
        ImageView img_love;
        ImageView img_remove;
        ImageView img_order;
    }
}